package com.fishery.processing.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.entity.Result;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.Input;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.service.CraftService;
import com.fishery.processing.service.impl.CraftServiceImpl;
import com.fishery.processing.util.ConvertUtil;
import com.fishery.processing.util.UpdateProxy;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.ws.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Api(tags = "加工工艺相关接口")
@CrossOrigin
@RestController
@RequestMapping("/processingFactory/craft")
public class CraftController {

    @Autowired
    private CraftService craftService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private ConvertUtil convertUtil;

    @PostMapping("/")
    @ApiOperation("新增工艺")
    public Result createCraft(@Valid @RequestBody @ApiParam(required = true,value = "craftVo") CraftVo craftVo){
        return craftService.createCraft(convertUtil.convert(craftVo,Craft.class)) == 1 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除工艺")
    public Result deleteCraft(@PathVariable(value = "id") String craftId){
        return craftService.deleteCraft(craftId) == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/")
    @ApiOperation("修改工艺")
    public Result updateCraft(@Valid @RequestBody @ApiParam(required = true,value = "craftVo") CraftVo craftVo){
        Craft craft = craftService.findCraftById(craftVo.getId());
        BeanUtils.copyProperties(craftVo,craft);
        //使用反射对修改操作进行检查
        return ((CraftService)(updateProxy.bind(craftService).getProxyInstance())).updateCraft(craft) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据工艺id获取加工工艺")
    public Result searchCraftById(@PathVariable(value = "id") String craftId){
        //把查询出的对象转成VO
        return Result.success().data(convertUtil.convert(craftService.searchCraftById(craftId),CraftVo.class));
    }

    @PostMapping("/{craftId}/{inputId}")
    @ApiOperation("添加工艺的投入品")
    public Result addInput(@PathVariable(value = "craftId") String craftId
                            ,@PathVariable(value = "inputId") String inputId){
        return craftService.addInput(craftId,inputId) == 1 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @DeleteMapping("/{craftId}/{inputId}")
    @ApiOperation("删除工艺的投入品")
    public Result deleteInput(@PathVariable(value = "craftId") String craftId
                                ,@PathVariable(value = "inputId") String inputId){
        return craftService.removeInput(craftId,inputId) == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/productCraft/{id}/{page}/{size}")
    @ApiOperation("分页获取加工产品的工艺")
    public Result searchCraftsOfProduct(@PathVariable(value = "id") String productId
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size){
        return Result.success().
                data(convertUtil.convert(craftService.searchCraftsOfProduct(productId, page, size),CraftVo.class));
    }

    @GetMapping("/productCraft/{id}")
    @ApiOperation("获取加工产品的工艺")
    public Result searchCraftsOfProduct(@PathVariable(value = "id") String productId){
        return Result.success().data(convertUtil.convert(craftService.searchCraftsOfProduct(productId),CraftVo.class));
    }

    @GetMapping("/factoryCraft/{id}/{page}/{size}")
    @ApiOperation("分页获取加工厂的工艺")
    public Result searchCraftsOfFactory(@PathVariable(value = "id") String factoryId
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size) {
        return Result.success().
                data(convertUtil.convert(craftService.searchCraftsOfFactory(factoryId, page, size),CraftVo.class));
    }

    @GetMapping("/factoryCraft/{id}")
    @ApiOperation("获取加工厂的工艺")
    public Result searchCraftsOfFactory(@PathVariable(value = "id") String factoryId) {
        return Result.success().data(convertUtil.convert(craftService.searchCraftsOfFactory(factoryId),CraftVo.class));
    }
}

